package com.tanhua.server.service;


import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class MovementsMQService {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    /**
     * 发送消息公共方法
     * publishId 动态id
     * type 类型 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
     */
    public void sendMsg(String publishId, Integer type){
        // 根据动态id查询动态
        Publish publish = publishApi.findById(publishId);

        // 封装消息内容
        Map<String, Object> msg = new HashMap<>();
        msg.put("userId", UserHolder.getUserId());
        msg.put("pid", publish.getPid());
        msg.put("publishId", publishId);
        msg.put("type", type);

        rocketMQTemplate.convertAndSend("quanzi_publish", JSON.toJSONString(msg));

    }
}
